package com.zx._02_thread.线程池;

/**
 * @version v1.0
 * @Project: 20180808_thread
 * @Title: Lession_4_ThreadLocal
 * @Description: 使用ThreadLocal保存静态方法之间使用同一个变量的
 * @author: zhangxue
 * @date: 2018年8月13日下午10:33:31
 * @company: yooli
 * @Copyright: Copyright (c) 2015
 */
public class Lession_4_ThreadLocal {

    private static final ThreadLocal<String> MY_VAR = new ThreadLocal<>();

    public static String getVar() {
        return MY_VAR.get();
    }

    public static void setVar(String var) {
        MY_VAR.set(var);
    }

    public static void main(String[] args) {

        //向线程池中提交两个线程去执行。
        for (int i = 0; i < 10; i++) {
            final int j = i;
            //创建一个线程
            new Thread(() -> {
                try {
                    System.out.println(
                            ">>>>>>>>>>>" + j + j + j + j + j + j + j + j + j + j + j + j + j + j + j + j + j + j);
                    Lession_4_ThreadLocal.setVar(Thread.currentThread().getName());
                    Thread.sleep(1000L);
                    System.out.println(Thread.currentThread().getName() + "获得自己的变量：" + Lession_4_ThreadLocal.getVar());
                    System.out.println(
                            "<<<<<<<<<<<" + j + j + j + j + j + j + j + j + j + j + j + j + j + j + j + j + j + j);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }

    }
}
